/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.matuschek.examples;

//import net.matuschek.examples.Test_multipleLinks;
import com.example.LinkReader;
import java.awt.BorderLayout;
import java.awt.CheckboxGroup;
import java.awt.Font;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.matuschek.jobo.JoBoSwing;



/**
 *
 * @author user
 */
public class LinkSelectWindow {


         private JFrame frame = new JFrame();
         private JPanel panelForList = new JPanel();
         private JPanel panelForButtons = new JPanel();
         private JLabel infoText = new JLabel();
         private JLabel linkDepthLable = new JLabel();
         private JPanel comboPanel = new JPanel();

    private CheckboxGroup checkboxGroup = new CheckboxGroup();
    private JCheckBox currentCheckBox;
    private Vector checkBoxVector = new Vector();
    private Vector LinkVector;
    private Vector SelectedLinks;
    private testMultipleLinks testMultipleLinks;
    private JoBoSwing joboSwing;
    private JFileChooser jFileChooser;
    private JComboBox comboBox;
    private int linkDepth;
    //private Checkbox currentCheckBox;

    public LinkSelectWindow(LinkReader linkReader) {

        SelectedLinks = new Vector();
        linkReader.getUrlList();
        LinkVector = linkReader.getUrlList();
        //LinkVector = v;
        initcomponents();
        
        frame.setLocationRelativeTo(null);
         frame.setSize(400, 200);
         frame.setVisible(true);

    }

    private void initcomponents(){



        System.out.println("LinkSelectSwing2_initicomponents method running");

         //Text in the top
         infoText.setText("Select the URLs you want to download and click next");
         infoText.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
         frame.getContentPane().add(BorderLayout.NORTH, infoText);

            //This creates checkBoxes according to the URL List
            String currentURL;

            for (int i=0; i<LinkVector.size(); i++){

                currentURL = (String) LinkVector.get(i);
                currentCheckBox = new JCheckBox();
                currentCheckBox.setText(currentURL);
                currentCheckBox.setSelected(true);
                checkBoxVector.add(currentCheckBox);
                panelForList.add(currentCheckBox);
            }

         panelForList.setLayout(new BoxLayout(panelForList, BoxLayout.Y_AXIS));
         frame.add(new JScrollPane(panelForList));


         linkDepthLable.setText("Link Depth: ");
         String[] comboItems = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
         comboBox = new JComboBox(comboItems);
         comboBox.setSelectedIndex(0);
         panelForButtons.add(linkDepthLable);
         panelForButtons.add(comboBox);
         //comboPanel.add(linkDepthLable);
         //comboPanel.add(comboBox);
         //frame.getContentPane().add(BorderLayout.SOUTH, comboPanel);
         linkDepth =  Integer.parseInt(comboBox.getSelectedItem().toString());




         //Bottom buttons
         JButton refresh = new JButton();
         JButton next = new JButton();
         next.setText("Next");
         panelForButtons.add(next);

            next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
            });

         frame.getContentPane().add(BorderLayout.SOUTH, panelForButtons);

         frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    }
    
    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {

//        for (int i=0; i < checkBoxVector.size(); i++){
//            JCheckBox tempCheckBox;
//            tempCheckBox = (JCheckBox) checkBoxVector.get(i);
//            if(tempCheckBox.isSelected()){
//                SelectedLinks.add(tempCheckBox.getText());
//            }
//        }
//
//        System.out.println(SelectedLinks);
//
//        joboSwing = new JoBoSwing(SelectedLinks);
//
        frame.setVisible(false);
//        joboSwing.miniTest();

        jFileChooser = new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jFileChooser.setDialogTitle("Select the folder to where pages should be saved");

        jFileChooser.setVisible(true);
        int returnVal=jFileChooser.showOpenDialog(frame);
        if(returnVal==JFileChooser.APPROVE_OPTION){
            
            String filePath = jFileChooser.getSelectedFile().getAbsolutePath();


        for (int i=0; i < checkBoxVector.size(); i++){
            JCheckBox tempCheckBox;
            tempCheckBox = (JCheckBox) checkBoxVector.get(i);
            if(tempCheckBox.isSelected()){
                SelectedLinks.add(tempCheckBox.getText());
            }
        }

        System.out.println(SelectedLinks);

        joboSwing = new JoBoSwing(SelectedLinks, filePath, linkDepth);
        joboSwing.miniTest();

        }

    }



}


